home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / scfdgen.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  8KB  |  203 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* scfdgen.c */
  20. /* Generate the CCITTFaxDecode tables */
  21. #include "stdio_.h"    /* includes std.h */
  22. #include "scf.h"
  23. #include "malloc_.h"
  24. #include "memory_.h"
  25.  
  26. typedef void (*cfd_node_proc)(P6(cfd_node *, cfd_node *,
  27.                  uint, int, int, int));
  28. typedef void (*cfd_enum_proc)(P4(cfd_node_proc,
  29.                  cfd_node *, cfd_node *, int));
  30. private void cfd_build_tree(P4(cfd_node *, cfd_enum_proc, int, FILE *));
  31. private void cfd_enumerate_white(P4(cfd_node_proc,
  32.                     cfd_node *, cfd_node *, int));
  33. private void cfd_enumerate_black(P4(cfd_node_proc,
  34.                     cfd_node *, cfd_node *, int));
  35. private void cfd_enumerate_2d(P4(cfd_node_proc,
  36.                  cfd_node *, cfd_node *, int));
  37. private void cfd_enumerate_uncompressed(P4(cfd_node_proc,
  38.                        cfd_node *, cfd_node *, int));
  39.  
  40. main()
  41. {    FILE *out = fopen("scfdtab.c", "w");
  42.     cfd_node area[1 << max(cfd_white_initial_bits, cfd_black_initial_bits)];
  43.     fputs("/* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved. */\n\n", out);
  44.     fputs("/* This file is part of Ghostscript.  It is licensed under the same */\n", out);
  45.     fputs("/* terms as the rest of Ghostscript.  If you do not have Ghostscript, */\n", out);
  46.     fputs("/* you do not have the right to have this file. */\n\n", out);
  47.     fputs("/* scfdtab.c */\n", out);
  48.     fputs("/* Tables for CCITTFaxDecode filter. */\n\n", out);
  49.     fputs("#include \"std.h\"\n", out);
  50.     fputs("#include \"scf.h\"\n\n", out);
  51.     fputs("/* White decoding table. */\n", out);
  52.     fputs("const cfd_node far_data cf_white_decode[] = {\n", out);
  53.     cfd_build_tree(area, cfd_enumerate_white, cfd_white_initial_bits, out);
  54.     fputs("\n};\n\n", out);
  55.     fputs("/* Black decoding table. */\n", out);
  56.     fputs("const cfd_node far_data cf_black_decode[] = {\n", out);
  57.     cfd_build_tree(area, cfd_enumerate_black, cfd_black_initial_bits, out);
  58.     fputs("\n};\n\n", out);
  59.     fputs("/* 2-D decoding table. */\n", out);
  60.     fputs("const cfd_node far_data cf_2d_decode[] = {\n", out);
  61.     cfd_build_tree(area, cfd_enumerate_2d, cfd_2d_initial_bits, out);
  62.     fputs("\n};\n\n", out);
  63.     fputs("/* Uncompresssed decoding table. */\n", out);
  64.     fputs("const cfd_node far_data cf_uncompressed_decode[] = {\n", out);
  65.     cfd_build_tree(area, cfd_enumerate_uncompressed, cfd_uncompressed_initial_bits, out);
  66.     fputs("\n};\n\n", out);
  67.     fputs("/* Dummy executable code to pacify compilers. */\n", out);
  68.     fputs("void\ncfd_dummy()\n{\n}\n", out);
  69.     fclose(out);
  70.     return 0;
  71. }
  72.  
  73. /* Initialize first-level leaves, count second-level nodes. */
  74. private void
  75. cfd_count_nodes(cfd_node *tree, cfd_node *ignore_extn,
  76.   uint code, int code_length, int run_length, int initial_bits)
  77. {    if ( code_length <= initial_bits )
  78.     {    /* Initialize one or more first-level leaves. */
  79.         int sh = initial_bits - code_length;
  80.         cfd_node *np = &tree[code << sh];
  81.         int i;
  82.         for ( i = 1 << sh; i > 0; i--, np++ )
  83.             np->run_length = run_length,
  84.             np->code_length = code_length;
  85.     }
  86.     else
  87.     {    /* Note the need for a second level. */
  88.         cfd_node *np = &tree[code >> (code_length - initial_bits)];
  89.         np->code_length = max(np->code_length, code_length);
  90.     }
  91. }
  92.  
  93. /* Initialize second-level nodes. */
  94. private void
  95. cfd_init2_nodes(cfd_node *tree, cfd_node *extn,
  96.   uint code, int code_length, int run_length, int initial_bits)
  97. {    int xbits = code_length - initial_bits;
  98.     int xrep;
  99.     cfd_node *np1, *np2;
  100.     int i;
  101.     if ( xbits <= 0 ) return;
  102.     np1 = &tree[code >> xbits];
  103.     np2 = &extn[np1->run_length - (1 << initial_bits)];
  104.     xrep = np1->code_length - code_length;
  105.     i = 1 << xrep;
  106.     np2 += (code & ((1 << xbits) - 1)) * i;
  107.     for ( ; i > 0; i--, np2++ )
  108.         np2->run_length = run_length,
  109.         np2->code_length = xbits;
  110. }
  111.  
  112. /* Enumerate all the relevant white or black codes. */
  113. private void
  114. cfd_enumerate_codes(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  115.   int initial_bits, const cfe_run *tt, const cfe_run *mut)
  116. {    int i;
  117.     const cfe_run *ep;
  118.     for ( i = 0, ep = tt; i < 64; i++, ep++ )
  119.       (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  120.     for ( i = 1, ep = mut + 1; i < 41; i++, ep++ )
  121.       (*proc)(tree, extn, ep->code, ep->code_length, i << 6, initial_bits);
  122.     (*proc)(tree, extn, cf1_run_uncompressed.code, cf1_run_uncompressed.code_length, run_uncompressed, initial_bits);
  123.     (*proc)(tree, extn, 0, run_eol_code_length - 1, run_zeros, initial_bits);
  124. }
  125. private void
  126. cfd_enumerate_white(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  127.   int initial_bits)
  128. {    cfd_enumerate_codes(proc, tree, extn, initial_bits,
  129.                 cf_white_termination, cf_white_make_up);
  130. }
  131. private void
  132. cfd_enumerate_black(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  133.   int initial_bits)
  134. {    cfd_enumerate_codes(proc, tree, extn, initial_bits,
  135.                 cf_black_termination, cf_black_make_up);
  136. }
  137.  
  138. /* Enumerate the 2-D codes. */
  139. private void
  140. cfd_enumerate_2d(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  141.   int initial_bits)
  142. {    int i;
  143.     const cfe_run *ep;
  144.     (*proc)(tree, extn, cf2_run_pass.code, cf2_run_pass.code_length,
  145.         run2_pass, initial_bits);
  146.     (*proc)(tree, extn, cf2_run_horizontal.code, cf2_run_horizontal.code_length,
  147.         run2_horizontal, initial_bits);
  148.     for ( i = 0; i < countof(cf2_run_vertical); i++ )
  149.     {    ep = &cf2_run_vertical[i];
  150.         (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  151.     }
  152.     (*proc)(tree, extn, cf2_run_uncompressed.code, cf2_run_uncompressed.code_length,
  153.         run_uncompressed, initial_bits);
  154.     (*proc)(tree, extn, 0, run_eol_code_length - 1, run_zeros, initial_bits);
  155. }
  156.  
  157. /* Enumerate the uncompressed codes. */
  158. private void
  159. cfd_enumerate_uncompressed(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  160.   int initial_bits)
  161. {    int i;
  162.     const cfe_run *ep;
  163.     for ( i = 0; i < countof(cf_uncompressed); i++ )
  164.     {    ep = &cf_uncompressed[i];
  165.         (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  166.     }
  167.     for ( i = 0; i < countof(cf_uncompressed_exit); i++ )
  168.     {    ep = &cf_uncompressed_exit[i];
  169.         (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  170.     }
  171. }
  172.  
  173. /* Build and write out the table. */
  174. private void
  175. cfd_build_tree(cfd_node *tree, cfd_enum_proc enum_proc, int initial_bits,
  176.   FILE *f)
  177. {    cfd_node *np;
  178.     const char *prev = "";
  179.     int i, next;
  180.     cfd_node *extn;
  181.     memset(tree, 0, sizeof(cfd_node) << initial_bits);
  182.     /* Construct and write the first level of the tree. */
  183.     (*enum_proc)(cfd_count_nodes, tree, (cfd_node *)0, initial_bits);
  184.     next = 0;
  185.     for ( i = 0, np = tree; i < 1 << initial_bits; i++, np++ )
  186.     { if ( np->code_length > initial_bits )        /* second level needed */
  187.       { np->run_length = next + (1 << initial_bits);
  188.         next += 1 << (np->code_length - initial_bits);
  189.       }
  190.       fprintf(f, "%s\t{ %d, %d }", prev, np->run_length, np->code_length);
  191.       prev = ",\n";
  192.     }
  193.     /* Construct and write the second level. */
  194.     extn = (cfd_node *)malloc(sizeof(cfd_node) * next);
  195.     for ( i = 0, np = extn; i < next; i++, np++ )
  196.         np->run_length = run_error,
  197.         np->code_length = 0;
  198.     (*enum_proc)(cfd_init2_nodes, tree, extn, initial_bits);
  199.     for ( i = 0, np = extn; i < next; i++, np++ )
  200.       fprintf(f, ",\n\t{ %d, %d }", np->run_length, np->code_length);
  201.     free((char *)extn);
  202. }
  203.